<template>


	<a-row :gutter="10" class="mb-2">
		<a-col :span="24">
			<a-card :bordered="false">
				<a-space>
					<a-input-search placeholder="请输入内容" v-model:value="config.search" @search="refresh">
					</a-input-search>
					<a-button @click="add" type="primary">新增</a-button>
				</a-space>

			</a-card>
		</a-col>
	</a-row>

	<a-row :gutter="10">
		<a-col :span="24">
			<a-card :bordered="false">
				<h-table :columns="columns" height="calc(70vh)" ref="tableRef" :data="queryList">



				</h-table>

			</a-card>
		</a-col>
	</a-row>


	<drag-modal v-model:visible="config.threadDiag.visible" width="300px" :closable="false"
		@cancel="config.threadDiag.visible = false" @ok="saveThread">
		<a-form :model="threadDiag">
			<a-form-item label="线程名称">
				<a-input v-model:value="config.threadDiag.threadName"></a-input>
			</a-form-item>
			<a-form-item label="线程编码">
				<a-input v-model:value="config.threadDiag.threadCode"></a-input>
			</a-form-item>
			<a-form-item label="核心线程数">
				<a-input-number v-model:value="config.threadDiag.corePoolSize"></a-input-number>
			</a-form-item>
			<a-form-item label="最大线程数">
				<a-input-number v-model:value="config.threadDiag.maximumPoolSize"></a-input-number>
			</a-form-item>
			<a-form-item label="连接保持时间">
				<a-input-number v-model:value="config.threadDiag.keepAliveTime"></a-input-number>
			</a-form-item>
			<a-form-item label="超时时间单位">
				<a-select v-model:value="config.threadDiag.timeUnit" placeholder="Select">
					<a-select-option v-for="item in config.timeUnitList" :key="item.code" :label="item.name"
						:value="item.code" />
				</a-select>
			</a-form-item>
			<a-form-item label="队列大小">
				<a-input-number v-model:value="config.threadDiag.queueCapacity"></a-input-number>
			</a-form-item>
			<a-form-item label="拒绝策略">
				<a-select v-model:value="config.threadDiag.rejectPolicy" placeholder="Select">
					<a-select-option v-for="item in config.rejectPolicyList" :key="item.code" :label="item.name"
						:value="item.code" />
				</a-select>
			</a-form-item>
		</a-form>
	</drag-modal>

	<drag-modal v-model:visible="config.threadStackTrace" width="100%" :closable="false"
	height="400px" @ok="config.threadStackTrace = false"
		@cancel="config.threadStackTrace = false">
		<a-tabs v-model:activeKey="config.threadTabActive" class="demo-tabs">
			<a-tab-pane :tab="index" :name="index" v-for="(item, index) in config.threadInfo" :key="index">
				<span v-html="item"></span>
			</a-tab-pane>
		</a-tabs>
	</drag-modal>

</template>

<script setup>
import threadApi from '@/api/thread/threadApi'
import { message } from 'ant-design-vue';

const config = ref({
	tableData: [],
	timeUnitList: [],
	rejectPolicyList: [],
	search: null,
	threadTabActive: null,
	threadInfo: null,
	threadStackTrace: false,
	initStr: '',
	threadDiag: {
		viewFlag: false,
		visible: false,
		threadName: '',
		threadCode: '',
		id: null,
		corePoolSize: 8,
		maximumPoolSize: 20,
		keepAliveTime: 5,
		queueCapacity: 5000,
		timeUnit: 'SECONDS',
		rejectPolicy: 'ABORT'
	}
})
onMounted(() => {
	queryTimeUnitList();
	queryRejectPolicyList();
})

const columns = ref([
	{
		title: '名称',
		key: 'threadName',
		width: 100,
		dataIndex: 'threadName',
		ellipsis: true,
	},
	{
		title: '编码',
		key: 'threadCode',
		dataIndex: 'threadCode',
		ellipsis: true,
	},
	{
		title: '核心线程数',
		key: 'corePoolSize',
		dataIndex: 'corePoolSize',
		ellipsis: true,
	},
	{
		title: '最大线程数',
		key: 'maximumPoolSize',
		dataIndex: 'maximumPoolSize',
		ellipsis: true,
	},
	{
		title: '队列大小',
		key: 'queueCapacity',
		dataIndex: 'queueCapacity',
		ellipsis: true,
	},
	{
		title: '拒绝策略',
		key: 'rejectPolicy',
		dataIndex: 'rejectPolicy',
		ellipsis: true,
	},
	{
		title: '活动线程数',
		key: 'activeCount',
		dataIndex: 'activeCount',
		ellipsis: true,
	},
	{
		title: '等待队列数',
		key: 'waiteQueueCount',
		dataIndex: 'waiteQueueCount',
		ellipsis: true,
	},
	{
		title: '总共完成任务数',
		key: 'completedTaskCount',
		dataIndex: 'completedTaskCount',
		ellipsis: true,
	},
	{
		title: '总共任务数',
		key: 'taskCount',
		dataIndex: 'taskCount',
		ellipsis: true,
	},
	{
		title: '启停状态',
		key: 'disableFlag',
		dataIndex: 'disableFlag',
		ellipsis: true,
		mapping: [{
			key: '1',
			value: '停用'
		},
		{
			key: '0',
			value: '启用'
		}],
		fixed: 'right'
	},
	{
		title: '操作',
		key: 'action',
		dataIndex: 'action',
		ellipsis: true,
		width: 320,
		fixed: 'right',
		buttonList: [
			{
				click: (field, record) => {
					handleClick(record)
				},
				render: (record) => {
					if (record.id != 'webThreadPool' && record.id != 'processInstancePool') {
						return "查看";
					}

				}
			},
			{
				type: 'primary',
				click: (field, record) => {
					edit(record)
				},
				render: (record) => {
					if (record.id != 'webThreadPool' && record.id != 'processInstancePool') {
						return "编辑";
					}

				}
			},
			{
				click: (field, record) => {
					setStatusFlag(record)
				},
				render: (record) => {
					if (record.id != 'webThreadPool' && record.id != 'processInstancePool') {
						return record.disableFlag == '0' ? '停用' : '启用';
					}

				}
			},
			{
				click: (field, record) => {
					handleThreadInfo(record)
				},
				render: (record) => {
					if (record.id != 'processInstancePool') {
						return "线程堆栈";
					}

				}
			}

		]
	}

])

const tableRef = ref();
const refresh = () => {
	//queryList();
	tableRef.value.refresh(true)
}
const add = () => {
	config.value.threadDiag = {}
	config.value.threadDiag.visible = true;

}
const setStatusFlag = async (row) => {
	await threadApi.setStatusFlag({
		id: row.id, disableFlag:
			row.disableFlag == '1' ? '0' : '1'
	});
	refresh();
}
const handleClick = async (row) => {
	await edit(row);
	config.value.threadDiag.viewFlag = true;
}
const handleThreadInfo = async (row) => {
	const d = await threadApi.threadStackTrace({
		id: row.id
	});
	config.value.threadInfo = d;

	var keys = Object.keys(d);
	if (keys) {
		config.value.threadTabActive = keys[0];
		keys.forEach(value => {
			var s = config.value.threadInfo[value];
			s = s.replace(/\r\n/g, "<br/>")
			s = s.replace(/\n/g, "<br/>");
			config.value.threadInfo[value] = s;
		})
	}
	config.value.threadStackTrace = true;
}

const edit = async (row) => {
	const data = await threadApi.getById({ id: row.id });
	config.value.threadDiag = data;
	config.value.threadDiag.visible = true;
}
const saveThread = async () => {
	if (config.value.threadDiag.viewFlag) {
		config.value.threadDiag.viewFlag = false;
		config.value.threadDiag.visible = false;
		return
	}
	if (!config.value.threadDiag.threadName) {
		message.error('请输入名称');
		return;
	}
	if (!config.value.threadDiag.threadCode) {
		message.error('请输入编码');
		return;
	}
	if (!config.value.threadDiag.corePoolSize) {
		message.error('请输入核心线程数');
		return;
	}
	if (!config.value.threadDiag.maximumPoolSize) {
		message.error('请输入最大线程数');
		return;
	}
	if (!config.value.threadDiag.queueCapacity) {
		message.error('请输入队列大小');
		return;
	}
	if (!config.value.threadDiag.keepAliveTime) {
		message.error('请输入存活时间');
		return;
	}
	if (!config.value.threadDiag.timeUnit) {
		message.error('请选择超时时间单位');
		return;
	}
	if (!config.value.threadDiag.rejectPolicy) {
		message.error('请选择拒绝策略');
		return;
	}
	await threadApi.save(config.value.threadDiag);
	config.value.threadDiag = {}
	refresh();
}


const queryTimeUnitList = async () => {
	const data = await threadApi.queryTimeUnitList({});
	config.value.timeUnitList = data;
}
const queryRejectPolicyList = async () => {
	const data = await threadApi.queryRejectPolicyList({});
	config.value.rejectPolicyList = data;
}
const queryList = async () => {
	const data = await threadApi.list({})
	return data;
}
</script>

<style scoped></style>
